"use strict";

/// <reference path="../../../../../global.d.ts" />

/* global writeBackDIYForm */

/**
 * 函数用于在小类tag点击的时候触发
 * @param {string} val 添加DIY表单的小类tag名称，exp：银行
 */
function AddNewFreeOption(val) {
  layer.msg("加载中", {
    icon: 16,
    shade: 0.01
  });
  /** 获取对应小类id（数据库意义的id） */
  var uID = $("input#" + val).attr("value");
  /** 获取服务器上的表单数据 */
  var ajaxUrl = String($("#get_new_free_form").val());
  $.ajax(ajaxUrl, {
    method: "POST",
    data: { m_group: uID },
    success: function success(data) {
      // data.id 是数据库的表单id，至少目前看来是废的，已和后端确认
      // if (data.m_id) {
      if (data.formData.length > 0) {
        writeBackDIYForm(data);
      } else {
        addNewHiddenFree(11, uID, val);
      }
    },
    error: function error(data) {
      console.clear();
      console.log(data);
    }
  });
}
/** 用于移除指定的DIY表单 */
function RemoveNewFreeOption(val) {
  var uID = $("input#" + val).attr("value");
  var targetHiddenInput = $("input[value='" + uID + "']").filter(function() {
    if (/^hidden_free/.test($(this).attr("name"))) {
      return this;
    }
  });
  if (targetHiddenInput.length > 1) {
    alert("发生严重错误，请联系后台管理员");
    throw new Error("targetHiddenInput\u591A\u4E8E\u4E00\u4E2A");
  } else {
    targetHiddenInput.parent().remove();
    if ($("[inori-diy-data='" + uID + "']").length === 1) {
      $("[inori-diy-data='" + uID + "']").remove();
    } else if (targetHiddenInput.siblings(".button").length === 0) {
      var errorValue = $("[inori-diy-data='" + uID + "']").length.toString();
      throw new Error(errorValue);
    }
  }
}
/** 用于添加指定id的自由表单框框 */
function addNewHiddenFree(formID, uID, val) {
  /** formID 暂时想不出有什么情况需要 */
  var freeID = $("#hiddenFreeCounter").val();
  freeID++;

  var templ = ('\n<div id="hidden_free_' +
    freeID +
    '" class="hdden_free_wrap">\n  <div style="width:100%;height:1px;clear:both;"></div>\n  <div class = "wrap_title">\u5C0F\u7C7B\uFF1A' +
    val +
    '</div>\n  <div class="addNewHiddenFormLine">\n    <select\n      class="addNewHiddenFormLineType" form=\'not_submit\'\n      aria-multiple = \'false\'\n      aria-placeholder="\u8BF7\u9009\u62E9\u65B0\u589E\u8868\u5355\u7C7B\u578B"\n    >\n      <option></option>\n      <optgroup label="">\n        <option value="1">\u6570\u5B57\u8F93\u5165</option>\n      </optgroup>\n      <optgroup label="">\n        <option value="2">\u5355\u9009</option>\n      </optgroup>\n      <optgroup label="">\n        <option value="3">\u591A\u9009</option>\n      </optgroup>\n    </select>\n    <div class="button">\n        <button onclick=\'addNewHiddenFormLine(this, event)\'>\u6DFB\u52A0</button>\n    </div>\n  </div>\n  <input type="hidden" class=\'hiddenLineCounter\' name="lineCounter" value="3" form=\'not_submit\'>\n  <input type="hidden" name="hidden_free[1][id]" value="' +
    uID +
    '">\n  <input type="hidden" name="hidden_free[1][value]" value="' +
    val +
    "\">\n\n  <!-- \u6700\u540E\u4E00\u4E2A\u5143\u7D20\u5FC5\u987B\u662Fdiv\uFF0C\u63D2\u5165\u5143\u7D20\u5B9A\u4F4D\u4E3A\u6700\u540E\u4E00\u4E2Adiv\u4E4B\u524D -->\n  <div class=\"button\">\n    \u9875\u9762\u63D0\u4EA4\u524D\u5FC5\u987B\u5148\u63D0\u4EA4\u672C\u533A\u57DF\uFF01\n    <br>\n    <button onclick='postThisHiddenLine(this, event)'>\u4FDD\u5B58</button>\n  </div>\n</div>\n<script id = 'addFreeScript'>\n  $('#hidden_free_" +
    freeID +
    " .addNewHiddenFormLineType').select2({\n    placeholder: '\u8BF7\u9009\u62E9\u65B0\u589E\u8868\u5355\u7C7B\u578B',\n    tags: false,\n    multiple: false,\n    width: 'element'\n  })\n  setTimeout(()=>{$('#addFreeScript').remove()}, 0)\n</script>\n  ").trim();
  $("#hdden_free_form_wrap").append(templ);
  $("#hiddenFreeCounter").val(freeID);
}
/** 用于添加指定类型的DIY表单行，也就是一个问题 */
function addNewHiddenFormLine(_this, evt) {
  var nowFormID = $(_this)
    .closest(".addNewHiddenFormLine")
    .parent()
    .attr("id")
    .match(/\d+/)[0];
  /** 一级数值 */
  var freeCounter = nowFormID;
  /** 二级数值 */
  var lineCounter = $(_this)
    .closest(".addNewHiddenFormLine")
    .siblings(".hiddenLineCounter")
    .val();
  lineCounter++;
  /** 数字输入模板 */
  var numberTempl = ('\n<div class="input_wrap number_input">\n  <div class="title"><input type="text" form=\'not_submit\' name="hidden_free[' +
    freeCounter +
    "][" +
    lineCounter +
    '][title]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    "_title\" placeholder=\"\u540D\u79F0\"></div>\n  <!-- \u56FA\u5B9A\u9690\u85CFinput\uFF0C\u7528\u4E8E\u540E\u7AEF\u5206\u8FA8\u8BE5\u884C\u8868\u5355\u7C7B\u578B -->\n  <input type='hidden' name='type' value='1'>\n  <div class=\"alt_data\">\n    <select\n      form = 'not_submit' name=\"hidden_free[" +
    freeCounter +
    "][" +
    lineCounter +
    '][alt_data]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    '_alt_data"\n      aria-multiple = \'false\'\n      aria-placeholder="\u8BF7\u9009\u62E9\u53D6\u503C\u65B9\u5411"\n    >\n      <option></option>\n      <optgroup label="">\n          <option value="\u5927\u4E8E">\u5927\u4E8E</option>\n          <option value="\u5927\u4E8E\u7B49\u4E8E">\u5927\u4E8E\u7B49\u4E8E</option>\n          <option value="\u5C0F\u4E8E">\u5C0F\u4E8E</option>\n          <option value="\u5C0F\u4E8E\u7B49\u4E8E">\u5C0F\u4E8E\u7B49\u4E8E</option>\n          <option value="\u7B49\u4E8E">\u7B49\u4E8E</option>\n        </optgroup>\n    </select>\n  </div>\n  <div class="main_data">\n    <input type="text" form=\'not_submit\' name="hidden_free[' +
    freeCounter +
    "][" +
    lineCounter +
    '][main_data]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    '_main_data" placeholder="\u6570\u503C" pattern="[\\d.]+">\n  </div>\n  <div class="button">\n    <button onclick=\'deleteThisHiddenLine(this)\'>\u5220\u9664</button>\n  </div>\n</div>\n<script id=\'tempmlScript\'>\n  $(\'#hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    "_alt_data').select2({\n    placeholder: '\u8BF7\u9009\u62E9\u53D6\u503C\u65B9\u5411',\n    tags: false,\n    multiple: false,\n    width: 'element'\n  })\n  setTimeout(()=>{$('#tempmlScript').remove()}, 0)\n</script>\n  ").trim();
  /** 单选模板 */
  var singleTempl = ('\n<div class="input_wrap single_select">\n  <div class="title"><input type="text" form=\'not_submit\' name="hidden_free[' +
    freeCounter +
    "][" +
    lineCounter +
    '][title]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    "_title\" placeholder=\"\u540D\u79F0\"></div>\n  <!-- \u56FA\u5B9A\u9690\u85CFinput\uFF0C\u7528\u4E8E\u540E\u7AEF\u5206\u8FA8\u8BE5\u884C\u8868\u5355\u7C7B\u578B -->\n  <input type='hidden' name='type' value='2'>\n  <div class=\"alt_data\">\n    <select\n      form = 'not_submit' name=\"hidden_free[" +
    freeCounter +
    "][" +
    lineCounter +
    '][alt_data]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    '_alt_data"\n    >\n      <option></option>\n      <optgroup label="\u6682\u65E0\u9009\u9879">\n        </optgroup>\n    </select>\n  </div>\n  <div class="main_data">\n      <select\n        form = \'not_submit\' name="hidden_free[' +
    freeCounter +
    "][" +
    lineCounter +
    '][main_data]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    "_main_data\"\n      >\n        <option inori-id='here-inori'></option>\n      </select>\n  </div>\n  <div class=\"button\" onclick='deleteThisHiddenLine(this)'>\n    <button onclick='deleteThisHiddenLine(this)'>\u5220\u9664</button>\n  </div>\n</div>\n<script id='tempmlScript'>\n  $('#hidden_free_" +
    freeCounter +
    "_" +
    lineCounter +
    "_alt_data').select2({\n    placeholder: '\u65E0\u9700\u9009\u62E9',\n    tags: false,\n    multiple: false,\n    width: 'element'\n  })\n  $('#hidden_free_" +
    freeCounter +
    "_" +
    lineCounter +
    "_main_data').select2({\n    placeholder: '\u8BF7\u9009\u62E9\u5177\u4F53\u9009\u9879',\n    tags: false,\n    multiple: false,\n    width: 'element'\n  })\n  setTimeout(()=>{$('#tempmlScript').remove()}, 0)\n</script>\n  ").trim();
  /** 多选模板 */
  var multipleTempl = ('\n<div class="input_wrap multiple_select">\n  <div class="title"><input type="text" form=\'not_submit\' name="hidden_free[' +
    freeCounter +
    "][" +
    lineCounter +
    '][title]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    "_title\" placeholder=\"\u540D\u79F0\"></div>\n  <!-- \u56FA\u5B9A\u9690\u85CFinput\uFF0C\u7528\u4E8E\u540E\u7AEF\u5206\u8FA8\u8BE5\u884C\u8868\u5355\u7C7B\u578B -->\n  <input type='hidden' name='type' value='3'>\n  <div class=\"alt_data\">\n    <select\n      form = 'not_submit' name=\"hidden_free[" +
    freeCounter +
    "][" +
    lineCounter +
    '][alt_data]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    '_alt_data"\n    >\n      <option></option>\n      <optgroup label="">\n          <option value="\u540C\u65F6\u7B26\u5408">\u540C\u65F6\u7B26\u5408</option>\n          <option value="\u4EFB\u610F\u7B26\u5408">\u4EFB\u610F\u7B26\u5408</option>\n        </optgroup>\n    </select>\n  </div>\n  <div class="main_data">\n      <select\n        form = \'not_submit\' name="hidden_free[' +
    freeCounter +
    "][" +
    lineCounter +
    '][main_data][]" id="hidden_free_' +
    freeCounter +
    "_" +
    lineCounter +
    "_main_data\"\n      >\n      <option inori-id='here-inori'></option>\n      </select>\n  </div>\n  <div class=\"button\" onclick='deleteThisHiddenLine(this)'>\n    <button onclick='deleteThisHiddenLine(this)'>\u5220\u9664</button>\n  </div>\n</div>\n<script id='tempmlScript'>\n  $('#hidden_free_" +
    freeCounter +
    "_" +
    lineCounter +
    "_alt_data').select2({\n    placeholder: '\u8BF7\u9009\u62E9\u5177\u4F53\u9009\u9879\uFF08\u5FC5\u586B\uFF09',\n    tags: false,\n    multiple: false,\n    width: 'element'\n  }).val(null).trigger('change')\n  $('#hidden_free_" +
    freeCounter +
    "_" +
    lineCounter +
    "_main_data').select2({\n    placeholder: '\u8BF7\u9009\u62E9\u5177\u4F53\u9009\u9879\uFF08\u5FC5\u586B\uFF09',\n    // tags: true,\n    multiple: true,\n    tokenSeparators: [',', ' '],\n    width: 'element',\n    closeOnSelect: false\n  }).val(null).trigger('change')\n  setTimeout(()=>{$('#tempmlScript').remove()}, 0)\n</script>\n  ").trim();

  var type = $(_this)
    .parent()
    .siblings(".addNewHiddenFormLineType")
    .val();
  if (type === "1") {
    // 数字输入
    $("#hidden_free_" + freeCounter + ">div:last-child").before(numberTempl);
  } else if (type === "2") {
    // 设定全局数据存放点
    var singleTemplName = "singleTempl";
    if (window.inori_var === undefined) window.inori_var = {};
    window.inori_var[singleTemplName] = singleTempl;
    // 弹层先填写选项
    openFreeSingleOption(singleTemplName);
    // 单选框
    // 异步的原因，下一步将会异步调用以下函数
    if (window.inori_func === undefined) window.inori_func = {};
    window.inori_func.addNewSinglLine = function(Arrayname, templName) {
      // Arrayname  为数组位置，目前是固定值，函数结束前undefined
      // templName  为模板位置，目前是固定值，函数结束前undefined
      var optionListArray = window[Arrayname];
      if (window.inori_var === undefined) window.inori_var = {};
      var templ = window.inori_var[templName];
      window[Arrayname] = undefined;
      window.inori_var[templName] = undefined;
      var optionList = '<optgroup label="">';
      // 去重数组
      var tempArray = [];
      $.each(optionListArray, function(e, i) {
        if (tempArray.indexOf(i.value) === -1) {
          tempArray.push(i.value);
          optionList +=
            "<option value='" +
            i.value +
            "' inori-data='" +
            i.name +
            "'>" +
            i.value +
            "</option>";
        }
      });
      optionList += "</optgroup>";
      templ = templ.replace(
        /<option inori-id='here-inori'><\/option>/,
        "<option></option>" + optionList
      );
      $("#hidden_free_" + freeCounter + ">div:last-child").before(templ);
    };
  } else if (type === "3") {
    // 设定全局数据存放点
    var MultipleTemplName = "MultipleTempl";
    if (window.inori_var === undefined) window.inori_var = {};
    window.inori_var[MultipleTemplName] = multipleTempl;
    // 弹层先填写选项
    openFreeMultipleOption(MultipleTemplName);
    // 多选框
    // 异步的原因，下一步将会异步调用以下函数
    if (window.inori_func === undefined) window.inori_func = {};
    window.inori_func.addNewMultiplelLine = function(Arrayname, templName) {
      // Arrayname  为数组位置，目前是固定值，函数结束前undefined
      // templName  为模板位置，目前是固定值，函数结束前undefined
      var optionListArray = window[Arrayname];
      if (window.inori_var === undefined) window.inori_var = {};
      var templ = window.inori_var[templName];
      window[Arrayname] = undefined;
      window.inori_var[templName] = undefined;
      var optionList = '<optgroup label="">';
      // 去重数组
      var tempArray = [];
      $.each(optionListArray, function(e, i) {
        if (tempArray.indexOf(i.value) === -1) {
          tempArray.push(i.value);
          optionList +=
            "<option value='" +
            i.value +
            "' inori-data='" +
            i.name +
            "'>" +
            i.value +
            "</option>";
        }
      });
      optionList += "</optgroup>";
      templ = templ.replace(
        /<option inori-id='here-inori'><\/option>/,
        "" + optionList
      );
      $("#hidden_free_" + freeCounter + ">div:last-child").before(templ);
    };
    // 复选框
    // $(`#hidden_free_${freeCounter}>div:last-child`).before(multipleTempl)
  } else {
    alert("请选择有效选项");
  }
  evt.preventDefault();
  // 执行完毕添加写入计数自增
  $(_this)
    .closest(".addNewHiddenFormLine")
    .siblings(".hiddenLineCounter")
    .val(lineCounter);
}
/** 用于移除当前指定的DIY表单行，也就是一个问题 */
function deleteThisHiddenLine(_this) {
  $(_this)
    .closest(".input_wrap")
    .remove();
  window.event.preventDefault();
}

/** 提交当前DIY表单 */
function postThisHiddenLine(_this, event, isSecond) {
  event.preventDefault();

  /** 获取类型为Number的表单(结构) */
  function getWholeNumber(_this, index, uID) {
    var title = !(
      $(_this)
        .find("[name$='[title]']")
        .val()
        .replace(/\s/g, "") === ""
    )
      ? $(_this)
          .find("[name$='[title]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */ console.log(
            $(_this)
              .find("[name$='[title]']")
              .val()
          );
        })();
    // 所有的alt_data都为固定值所以就不传具体参数了
    var altData = !(
      $(_this)
        .find("[name$='[alt_data]']")
        .val() === ""
    )
      ? $(_this)
          .find("[name$='[alt_data]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */
        })();
    var mainData = !(
      $(_this)
        .find("[name$='[main_data]']")
        .val()
        .replace(/\s/g, "") === ""
    )
      ? $(_this)
          .find("[name$='[main_data]']")
          .val()
          .replace(/\s/g, "")
      : void (function() {
          /* alert('请填写选项！') */ console.log(
            $(_this)
              .find("[name$='[title]']")
              .val()
          );
        })();
    return {
      input_type: 1,
      title: title,
      alt_data: altData,
      main_data: mainData,
      group: uID
    };
  }
  /** 获取类型为单选的表单(结构) */
  function getWholeSingleS(_this, index, uID) {
    var title = !(
      $(_this)
        .find("[name$='[title]']")
        .val()
        .replace(/\s/g, "") === ""
    )
      ? $(_this)
          .find("[name$='[title]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */
        })();
    // altData
    // let altData = $(_this).find(`[name$='\[alt_data\]']`).val()
    var mainData = $(_this)
      .find("[name$='[main_data]']")
      .find("option")
      .map(function() {
        if ($(this).attr("value")) {
          /** 自增数字 */
          var key = $(this).attr("inori-data");
          /** 用户输入的内容 */
          var value = $(this).attr("value");
          /** 用户输入的内容 */
          var text = $(this).text();
          var checked =
            $(_this)
              .find("[name$='[main_data]']")
              .val() === value;
          return {
            key: key,
            value: value,
            text: text,
            checked: checked,
            index: uID + "_" + index
          };
        }
      });
    // jq 对象不能用来提交
    var temp = [];
    mainData.each(function(index, ele) {
      temp.push(ele);
    });
    mainData = temp;
    if (mainData.length === 0) {
      // alert('请填写单选选项')
      return;
    }
    return {
      input_type: 2,
      title: title,
      alt_data: "",
      main_data: mainData,
      group: uID
    };
  }
  /** 获取类型为多选的表单(结构) */
  function getWholeMultipleS(_this, index, uID) {
    var title = !(
      $(_this)
        .find("[name$='[title]']")
        .val()
        .replace(/\s/g, "") === ""
    )
      ? $(_this)
          .find("[name$='[title]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */
        })();
    // altData为固定值，不传详细
    var altData = !(
      $(_this)
        .find("[name$='[alt_data]']")
        .val() === ""
    )
      ? $(_this)
          .find("[name$='[alt_data]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */
        })();
    var mainData = $(_this)
      .find("[name$='[main_data][]']")
      .find("option")
      .map(function() {
        if ($(this).attr("value")) {
          /** 自增数字 */
          var key = $(this).attr("inori-data");
          /** 用户输入的内容 */
          var value = $(this).attr("value");
          /** 用户输入的内容 */
          var text = $(this).text();
          var checked = !(
            $(_this)
              .find("[name$='[main_data][]']")
              .val()
              .indexOf(value) === -1
          );
          return {
            key: key,
            value: value,
            text: text,
            checked: checked,
            index: uID + "_" + index
          };
        }
      });
    if (mainData.length === 0) {
      // alert('请填写单选选项')
      return;
    }
    var temp = [];
    mainData.each(function(index, ele) {
      temp.push(ele);
    });
    mainData = temp;
    return {
      input_type: 3,
      title: title,
      alt_data: altData,
      main_data: mainData,
      group: uID
    };
  }

  /** 获取类型为Number的表单数据 */
  function getWholeNumberOnly(_this, index, uID) {
    // 所有的alt_data都为固定值所以就不传具体参数了
    var altData = !(
      $(_this)
        .find("[name$='[alt_data]']")
        .val() === ""
    )
      ? $(_this)
          .find("[name$='[alt_data]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */
        })();
    var mainData = !(
      $(_this)
        .find("[name$='[main_data]']")
        .val()
        .replace(/\s/g, "") === ""
    )
      ? $(_this)
          .find("[name$='[main_data]']")
          .val()
          .replace(/\s/g, "")
      : void (function() {
          /* alert('请填写选项！') */
        })();
    return {
      input_type: 1,
      m_group: uID + "_" + index,
      mainData: mainData,
      altData: altData,
      bq_id: uID
    };
  }
  /** 获取类型为单选的表单数据 */
  function getWholeSingleSOnly(_this, index, uID) {
    var mainData = $(_this)
      .find("[name$='[main_data]']")
      .find("option")
      .map(function() {
        if (
          $(_this)
            .find("[name$='[main_data]']")
            .val() === $(this).attr("value")
        ) {
          /** 自增数字 */
          var key = $(this).attr("inori-data");
          return key;
        }
      });
    if (mainData.length === 0) {
      // alert('请填写单选选项')
    } else {
      return {
        input_type: 2,
        m_group: uID + "_" + index,
        mainData: mainData[0],
        altData: "",
        bq_id: uID
      };
    }
  }
  /** 获取类型为多选的表单数据 */
  function getWholeMultipleSOnly(_this, index, uID) {
    var altData = !(
      $(_this)
        .find("[name$='[alt_data]']")
        .val() === ""
    )
      ? $(_this)
          .find("[name$='[alt_data]']")
          .val()
      : void (function() {
          /* alert('请填写选项！') */
        })();
    var mainData = $(_this)
      .find("[name$='[main_data][]']")
      .find("option")
      .map(function() {
        if (
          $(_this)
            .find("[name$='[main_data][]']")
            .val()
            .indexOf($(this).attr("value")) > -1
        ) {
          /** 自增数字 */
          var key = $(this).attr("inori-data");
          return key;
        }
      });
    // 不能提交jq对象数组
    var temp = [];
    mainData.each(function(index, ele) {
      temp.push(ele);
    });
    mainData = temp;
    if (altData === "" || mainData.length === 0) {
      /* alert('请填写相关多选选项') */
    } else {
      return {
        input_type: 3,
        m_group: uID + "_" + index,
        mainData: temp.join(","),
        altData: altData,
        bq_id: uID
      };
    }
  }
  var urlBase = $("#hidden_free_url_base").val();
  // 小类包裹
  var wholeWrap = $(_this).closest("div.hdden_free_wrap");
  // // 单行选择包裹（列表）
  var lineWrap = wholeWrap.children(".input_wrap");
  var hId = wholeWrap.children("input[name$='[id]']").val();
  var hValue = wholeWrap.children("input[name$='[value]']").val();
  var hData = [];
  var hDataOnly = [];
  lineWrap.each(function(index) {
    var type =
      $(this).children("[name='type']").length === 1
        ? $(this)
            .children("[name='type']")
            .val()
        : void function() {
            alert("严重错误");
          };
    var switchObj = {
      1: getWholeNumber,
      2: getWholeSingleS,
      3: getWholeMultipleS
    };
    var switchDataObj = {
      1: getWholeNumberOnly,
      2: getWholeSingleSOnly,
      3: getWholeMultipleSOnly
    };
    hData.push(switchObj[type](this, index, hId));
    hDataOnly.push(switchDataObj[type](this, index, hId));
  });
  var ajaxDta = {
    m_group: hId,
    m_value: hValue,
    formData: hData
  };
  var ajaxDtaAlt = {
    m_goods: location.href.match(/\/id\/(\d+)/)[1],
    m_goods_dID: location.href.match(/\/did\/(\d+)/)[1],
    m_group: hId,
    m_value: hValue,
    formData: hDataOnly
  };
  console.clear();
  console.log("提交的数据:");
  console.log("1、结构:");
  console.dir(ajaxDta);
  console.log("2、数据:");
  console.dir(ajaxDtaAlt);
  console.log("服务器返回的数据:");
  var shouldAbortSubmit = false;
  $.each(ajaxDtaAlt.formData, function(index, item) {
    if ("" + item === "undefined" || "" + item.mainData === "undefined") {
      shouldAbortSubmit = true;
    }
  });
  if (shouldAbortSubmit) {
    alert("请完整填写DIY表单");
    return;
  }
  var DIYPOSTajax = {};
  if (isSecond !== undefined && isSecond === "second") {
    var urlBaseAlt = String($("#hidden_free_data_url_base").val());
    postFIYData(urlBaseAlt);
  } else {
    DIYPOSTajax = $.ajax(String(urlBase), {
      method: "POST",
      data: ajaxDta,
      success: function success(data) {
        if (data.code === 1) {
          var _urlBaseAlt = String($("#hidden_free_data_url_base").val());
          postFIYData(_urlBaseAlt);
        } else {
          console.dir(data);
        }
      },
      error: function error() {
        alert("网络发生错误，请重试");
      }
    });
  }

  DIYPOSTajax._this = _this;
  DIYPOSTajax._uid = hId;
  function postFIYData(urlBaseAlt) {
    $.ajax(urlBaseAlt, {
      method: "POST",
      data: ajaxDtaAlt,
      success: function success(data) {
        if (data.code === 1) {
          console.log("提交成功");
          removeAltButton(DIYPOSTajax._this);
          var _value = data.data.join(",");
          var id =
            "<input type='hidden' name = 'check_xiaolei[]' inori-diy-data='" +
            DIYPOSTajax._uid +
            "' value='" +
            _value +
            "'>";
          $("#all").append(id);
        }
      },
      error: function error() {
        console.log("提交失败");
      }
    });
  }
}

/** 锁定相应DIY表单，隐藏修改相关的按钮 */
function removeAltButton(_this) {
  $(_this)
    .closest(".hdden_free_wrap")
    .find(".addNewHiddenFormLine")
    .remove();
  $(_this)
    .closest(".hdden_free_wrap")
    .find(".button")
    .remove();
}
